package com.khoayang.littlefighterchaos.menu;

import org.cocos2d.layers.CCLayer;
import org.cocos2d.nodes.CCDirector;
import org.cocos2d.nodes.CCLabel;
import org.cocos2d.nodes.CCSprite;
import org.cocos2d.types.CGPoint;
import org.cocos2d.types.CGRect;
import org.cocos2d.types.CGSize;
import org.cocos2d.types.ccColor3B;

public abstract class BaseControl {
	private CCSprite _Sprite;
	protected String _Image = "main-menu/button.png";
	protected int _Opacity = 128; 
	private CCLayer _Layer;
	private float _PosX;
	private float _PosY;
	private String _Title;
	protected String _StyleFont = "DroidSans";
	protected CCLabel _Label;
	private boolean _Enable = false;
	private BaseControl _ParentControl;
	
	public BaseControl()
	{
		set_Sprite(CCSprite.sprite(_Image));
		get_Sprite().setOpacity(_Opacity);
		
		CGSize winSize = CCDirector.sharedDirector().displaySize();
		set_PosX(winSize.width/2);
	}
	
	public void Draw()
	{
		get_Sprite().setOpacity(128);
		get_Sprite().setPosition(get_PosX(),get_PosY());
		get_Layer().addChild(get_Sprite());
		_Label = CCLabel.makeLabel(get_Title(), _StyleFont, 20);
		_Label.setColor(ccColor3B.ccBLACK);
		_Label.setPosition(get_PosX(),get_PosY());
		get_Layer().addChild(_Label);
		set_Enable(true);
	}
	
	public boolean checkTouch(float x, float y) {
		CGPoint convertedPoint = CCDirector.sharedDirector().convertToGL(
				CGPoint.ccp(x, y));
		
		CGPoint spriteTopLeftPos = CGPoint.ccp(get_Sprite().getPosition().x
				- get_Sprite().getContentSize().width / 2, get_Sprite().getPosition().y
				- get_Sprite().getContentSize().height / 2);
		CGRect spriteRect = new CGRect(spriteTopLeftPos,
				get_Sprite().getContentSize());
		if (spriteRect.contains(convertedPoint.x, convertedPoint.y))
			return true;
		return false;
	}
	
	public void UnDraw()
	{
		//get_Sprite().setOpacity(128);
		get_Sprite().removeSelf();
		_Label.removeSelf();
		set_Enable(false);
	}
	
	
	
	public abstract void add(BaseControl baseControl);
	
	public abstract void show();
	
	public abstract void hide();

	public boolean get_Enable() {
		return _Enable;
	}

	public void set_Enable(boolean _Enable) {
		this._Enable = _Enable;
	}

	public float get_PosX() {
		return _PosX;
	}

	public void set_PosX(float _PosX) {
		this._PosX = _PosX;
	}

	public String get_Title() {
		return _Title;
	}

	public void set_Title(String _Title) {
		this._Title = _Title;
	}

	public CCLayer get_Layer() {
		return _Layer;
	}

	public void set_Layer(CCLayer _Layer) {
		this._Layer = _Layer;
	}

	public float get_PosY() {
		return _PosY;
	}

	public void set_PosY(float _PosY) {
		this._PosY = _PosY;
	}

	public BaseControl get_ParentControl() {
		return _ParentControl;
	}

	public void set_ParentControl(BaseControl _ParentControl) {
		this._ParentControl = _ParentControl;
	}

	public CCSprite get_Sprite() {
		return _Sprite;
	}

	public void set_Sprite(CCSprite _Sprite) {
		this._Sprite = _Sprite;
	}
}
